#!/usr/bin/python
# -*- coding: UTF-8 -*-

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.RequestBase import OpenUBMCRestful


class IPMCSET_SNMP_001(Case):
    """
            Name:
                IPMCSET_SNMP_001
            Label:
                查询SNMP trap状态
            PreCondition:
                BMC正常启动
            TestStep:
                1) 命令行执行 ipmcget -t trap -d state -v 1查询snmp trap状态
            ExpectedResult:
                A) 状态查询和web端一致
            Parameters:

            Author:
                baixin liuya
            Last Modified:

            Remark:

        """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_password = self.bmc_host.password

        self.logger.info("初始化BMC Restful对象")
        self.bmc_restful = OpenUBMCRestful(self.bmc_ip, self.bmc_user, self.bmc_password)
        self.bmc_restful.login()

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 查询SNMP Trap功能信息")
        resp = self.bmc_host.run({"command": ["ipmcget -t trap -d state"], "timeout": 10})
        if resp['rc'] != 0:
            self.assertIsNotNone(None, f"{resp['stderr']}")
        snmp_trap_status = resp['stdout']
        if "enabled" in snmp_trap_status:
            snmp_trap_status = True

        self.log_step("Step2: 查询SNMP Trap网络管理系统状态")
        resp = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 1"], "timeout": 10})
        ipmcget_NMS_1 = resp['stdout']
        resp = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 2"], "timeout": 10})
        ipmcget_NMS_2 = resp['stdout']
        resp = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 3"], "timeout": 10})
        ipmcget_NMS_3 = resp['stdout']
        resp = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 1"], "timeout": 10})
        ipmcget_NMS_4 = resp['stdout']
        self.logger.info(f"网络管理系统状态\n序号1当前状态：{ipmcget_NMS_1}\n序号2当前状态：{ipmcget_NMS_2}\n序号3当前状态：{ipmcget_NMS_3}\n序号4当前状态：{ipmcget_NMS_4}")

        self.log_step("Step2: 查询Web页面trap状态")
        rp = self.bmc_restful.session.get(f"https://{self.bmc_ip}/UI/Rest/Maintenance/TrapNotification")
        if rp.status_code != 200:
            self.assertIsNotNone(None, "RestFul接口获取SNMP Trap信息失败")

        restful_snmp_trap_status = rp.json()["Enabled"]

        if snmp_trap_status == restful_snmp_trap_status:
            pass
        else:
            self.assertIsNotNone(None, f"SNMP Trap ipmcget获取的信息 与 WEB信息不一致")

        for trapserver in rp.json()["TrapServerList"]:
            if trapserver['ID'] == 0:
                restful_NMS_1 = trapserver['Enabled']
            if trapserver['ID'] == 1:
                restful_NMS_2 = trapserver['Enabled']
            if trapserver['ID'] == 2:
                restful_NMS_3 = trapserver['Enabled']
            if trapserver['ID'] == 3:
                restful_NMS_4 = trapserver['Enabled']

        self.log_step("Step3: 比较两者trap是否一致")
        if "disabled" in ipmcget_NMS_1:
            ipmcget_NMS_1 = False
        else:
            ipmcget_NMS_1 = True

        if "disabled" in ipmcget_NMS_2 and restful_NMS_2 == "false":
            ipmcget_NMS_2 = False
        else:
            ipmcget_NMS_2 = True

        if "disabled" in ipmcget_NMS_3 and restful_NMS_3 == "false":
            ipmcget_NMS_3 = False
        else:
            ipmcget_NMS_3 = True

        if "disabled" in ipmcget_NMS_4 and restful_NMS_4 == "false":
            ipmcget_NMS_4 = False
        else:
            ipmcget_NMS_4 = True

        if ipmcget_NMS_1 == restful_NMS_1 and ipmcget_NMS_2 == restful_NMS_2 and ipmcget_NMS_3 == restful_NMS_3 and \
            ipmcget_NMS_4 == restful_NMS_4:
            self.assertIsNotNone(None, "ipmcget结果与restful查询结果不相同")

        self.log_step("Step4: ipmcget结果与restful查询结果相同")

    # 测试结束后恢复环境
    def post_test_case(self):
        self.bmc_restful.logout()
